home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- **
- ** Folder Name: BS
- ** File Name: cdev.c
- **
- ** Copyright: © 1993 by Siren Enterprises, all rights reserved.
- **
- ** Description: Lil´ BeepSay control pannel, used to configure Lil´ BeepSay.
- **
- *******************************************************************************
- ** A U T H O R I D E N T I T Y
- *******************************************************************************
- **
- ** Initials Name
- ** -------- -----------------------------------------------
- ** KW Ken Wieschhoff
- **
- *******************************************************************************
- ** R E V I S I O N H I S T O R Y
- *******************************************************************************
- **
- ** Date Time Author Description
- ** -------- ----- ------ ---------------------------------------------
- ** 06/22/93 15:13 kw 1+ Add animate lips option
- ** 05/17/93 21:39 KW Original
- **
- ******************************************************************************/
-
- #include <Traps.h>
- #include <GestaltEqu.h>
- #include <Notification.h>
- #include <PLStringFuncs.h>
- #include <Quickdraw.h>
- #include <Packages.h>
- #include <errors.h>
- #include <Controls.h>
- #include <Menus.h>
- #include <Devices.h>
- #include "Resident.h"
- #include "Preferences.h"
- #include "Utilities.h"
-
-
-
- // •• Main Dialog items
- #define kOnButton 1
- #define kOffButton 2
- #define kVoiceMenu 4
- #define kText 5
- #define kTryText 6
- #define kShowIcon 7
- #define kDecorativeIcon 8
- #define kNoOverLap 9
- #define kFaster 10
- #define kSlower 11
- #define kLower 12
- #define kHigher 13
- #define kAnimate 14
-
- #define WhereAmI(a)
-
- //•• Protoypes
- OSErr DoInitialization(DialogPtr CPDialog,
- short numItems,
- long *cdevStorage);
-
- void DoClose(DialogPtr CPDialog, short numItems, long *cdevStorage);
-
- void DoItemHit(DialogPtr CPDialog,
- short item,
- short numItems,
- EventRecord *theEvent,
- long *cdevStorage);
-
- void DoUpdate( DialogPtr CPDialog,
- short numItems,
- long *cdevStorage);
-
- OSErr DoActivate(DialogPtr CPDialog,
- short numItems,
- long *cdevStorage);
-
- void DoDeactivate(long *cdevStorage);
-
- OSErr WritePreferences(PreferencesHdl preferences);
- pascal void ShowStr( StringPtr str);
- pascal void ShowStrNum( StringPtr str, OSErr error);
-
- pascal void SpeakAndShowText(DialogPtr CPDialog,
- short numItems,
- long *cdevStorage,
- StringPtr theText,
- Boolean animate);
-
- //•• Used to simulate speaking when text phrases are spoken
- struct callbackinfo {
- DialogPtr itsDialog;
- CIconHandle open;
- CIconHandle close;
- Rect itsRect;
- };
-
- typedef struct callbackinfo callbackinfo;
- typedef callbackinfo *callbackinfoPtr, **callbackinfoHdl;
-
-
- //•• Main entry point
- pascal long MAIN( short message,
- short item,
- short numItems,
- short /* CPanelID */,
- EventRecord *theEvent,
- long cdevStorage,
- DialogPtr CPDialog)
- {
- OSErr err;
-
- if( message == macDev) {
- return( 1); // we work on every machine
- }
-
- if (cdevStorage == 0)
- return (cdevStorage); // exit immediately, due to previous error
-
-
- switch( message) {
- case initDev:
- err = DoInitialization(CPDialog, numItems, &cdevStorage);
- break;
-
- case closeDev:
- DoClose(CPDialog, numItems, &cdevStorage);
-
- break;
-
- case hitDev:
- DoItemHit(CPDialog, item, numItems, theEvent, &cdevStorage);
- break;
-
- case nulDev:
- break;
-
- case updateDev:
- DoUpdate(CPDialog, numItems, &cdevStorage);
- break;
-
- case activDev:
- err = DoActivate(CPDialog, numItems, &cdevStorage);
- break;
-
- case deactivDev:
- DoDeactivate( &cdevStorage);
-
- break;
-
- case keyEvtDev:
- break;
-
- case undoDev:
- case cutDev:
- case copyDev:
- case pasteDev:
- case clearDev:
- break;
- }
-
-
- return( cdevStorage);
- }
-
-
- pascal void ShowStr( StringPtr str)
- {
- ParamText(str, "\p","\p","\p");
- (void) Alert(129, nil);
- }
-
- pascal void ShowStrNum( StringPtr str, OSErr error)
- {
- Str255 errorText;
-
- NumToString((long) error, errorText);
- ParamText( str, "\p. Error = ", errorText, "\p");
- (void) Alert(129, nil);
- }
-
-
-
- TrapType GetTrapType( short theTrap)
- {
- if ( theTrap & 0x0800)
- return ToolTrap;
- else
- return OSTrap;
- }
-
-
-
- Boolean HitCheckBox(DialogPtr dlg,
- short item)
- {
- short itemtype;
- ControlHandle hand;
- Rect box;
-
- GetDItem(dlg, item, &itemtype, (Handle*)&hand, &box);
- if (GetCtlValue(hand) == 0)
- {
- SetCtlValue(hand, 1);
- return(true);
- }
- else
- {
- SetCtlValue(hand, 0);
- return(false);
- }
- }
-
- Boolean GetRadioValue(DialogPtr dlg, short item)
- {
- short itemtype;
- ControlHandle hand;
- Rect box;
-
- GetDItem(dlg, item, &itemtype, (Handle*)&hand, &box);
- if (GetCtlValue(hand) == 0)
- {
- return(false);
- }
- else
- {
- return(true);
- }
- }
-
-
- void HitRadioButton(DialogPtr dlg,
- short item,
- short logicalID,
- short logicalStart,
- short logicalEnd)
- {
- short itemtype;
- ControlHandle hand;
- Rect box;
- short startID;
- short endID;
- short numItems;
- short loop;
-
- // Get the start and end ID's from the logicals
-
- numItems = item - logicalID;
- startID = logicalStart + numItems;
- endID = logicalEnd + numItems;
-
- // Clear all the radio buttons in the group
-
- for (loop = startID; loop <= endID; ++loop)
- {
- GetDItem(dlg, loop, &itemtype, (Handle*)&hand, &box);
- if (GetCtlValue(hand) != 0) SetCtlValue(hand, 0); // set control only when necessary
- }
-
- // Set the item hit
-
- GetDItem(dlg, item, &itemtype, (Handle*)&hand, &box);
- if (GetCtlValue(hand) != 1) SetCtlValue(hand, 1); // set control only when necessary
- }
-
-
-
- OSErr DoInitialization(DialogPtr CPDialog,
- short numItems,
- long *cdevStorage)
- {
- #pragma unused ( CPDialog, numItems)
-
- OSErr err;
- PreferencesHdl thePreferences;
- short menuItem = 1;
- long result;
-
-
- WhereAmI("\p DoInitialization");
-
- err = Gestalt( gestaltSpeechAttr, &result);
- if ((err != noErr) || !(result & (1 << gestaltSpeechMgrPresent))) {
- ShowStr("\p No Speech Manager Installed!");
- *cdevStorage = cdevGenErr;
- return(cdevGenErr);
- }
-
-
- // Read the preferences from disk.
- err = _ReadPreferences( &thePreferences, kBeepSayPrefs);
-
- if (err != noErr ) {
- ShowStrNum("\p Couldn't read/create preferences!", err);
- *cdevStorage = cdevGenErr;
- return(cdevGenErr);
- }
-
- *cdevStorage = (long *) thePreferences;
-
- }
-
-
-
- void DoDeactivate(long *cdevStorage)
- {
- #pragma unused ( cdevStorage)
- }
-
- void DoClose(DialogPtr CPDialog, short numItems, long *cdevStorage)
- {
- short itemType;
- Handle itemHandle;
- Rect box;
- PreferencesHdl thePreferences = (PreferencesHdl)*cdevStorage;;
-
-
- GetDItem( CPDialog, numItems + kText, &itemType, &itemHandle, &box);
- GetIText( itemHandle, (**thePreferences).theText);
-
- // Unconditionally write to preferences.
- WritePreferences( thePreferences);
- }
-
-
- pascal void PlotTheIcon(DialogPtr itsDialog, CIconHandle theIcon, Rect *itsRect)
- {
-
- // Support routine to simulate speaking when text phrases are spoken.
-
- GrafPtr oldPort;
-
- if ( theIcon) {
-
- GetPort( &oldPort);
- SetPort( itsDialog);
-
- PlotCIcon(itsRect, theIcon);
-
- SetPort( oldPort);
- }
- }
-
- pascal void EndOfTextCallback( SpeechChannel channel, long refCon)
- {
- # pragma unused( channel)
- callbackinfoPtr itsInfoPtr = (callbackinfoPtr) refCon;
-
- // Shut your mouth, dude!
- PlotTheIcon(itsInfoPtr->itsDialog, itsInfoPtr->close, &itsInfoPtr->itsRect);
-
- }
-
- pascal void PhonemeCallBack( SpeechChannel channel, long refCon,
- short phonemeOpcode)
- {
- # pragma unused(channel, phonemeOpcode )
- callbackinfoPtr itsInfoPtr = (callbackinfoPtr) refCon;
-
- // Open and then close the mouth for each phoneme.
- // •• Note that doing lengthy graphic operation here may introduce a "stutter"
- // while text phrases are spoken. Use small icons.
- PlotTheIcon(itsInfoPtr->itsDialog, itsInfoPtr->close, &itsInfoPtr->itsRect);
- PlotTheIcon(itsInfoPtr->itsDialog, itsInfoPtr->open, &itsInfoPtr->itsRect);
-
- }
-
- void DoItemHit(DialogPtr CPDialog,
- short item,
- short numItems,
- EventRecord *theEvent,
- long *cdevStorage)
- {
- #pragma unused( theEvent)
-
- short itemType;
- Handle itemHandle;
- Rect box;
- PreferencesHdl thePreferences = (PreferencesHdl)*cdevStorage;
- short itsVoice;
- Str255 theText;
- short logicalID;
- OSErr err = -214;
- StringPtr aboutText =
- "\p[[rate 150]]Version 1 point 0 point 1. Copyright Siren Enterprises. Narly, dude!";
- SpeechChannel itsChannel;
- long timeOut;
- Boolean animate = (**thePreferences).animate;
-
- logicalID = item - numItems;
-
- switch (logicalID)
- {
- case kOnButton:
- case kOffButton:
- (**thePreferences).on_off = (logicalID == kOnButton);
- HitRadioButton(CPDialog, item, item, kOnButton, kOffButton);
- break;
-
-
- case kVoiceMenu :
-
- GetDItem( CPDialog, item, &itemType, &itemHandle, &box);
- itsVoice = GetCtlValue( (ControlHandle)itemHandle);
- err = GetIndVoice(itsVoice, &(**thePreferences).theVoice);
- if ( err != noErr) {
- ShowStrNum("\p Could not get indicated voice!", err);
- return;
- }
-
- // Try for no more than 3 seconds to get a new speech channel.
- timeOut = TickCount() + 180;
- err = -1;
- while (( err != noErr) && (TickCount() < timeOut))
- err = NewSpeechChannel( &(**thePreferences).theVoice, &itsChannel);
-
- // Reset the default rate and pitch.
- if ( err == noErr) {
- (void) GetSpeechPitch( itsChannel, &(**thePreferences).thePitch);
- (void) GetSpeechRate( itsChannel, &(**thePreferences).theRate);
- (void) DisposeSpeechChannel( itsChannel);
- }
-
- break;
-
- case kTryText :
- GetDItem( CPDialog, numItems + kText, &itemType, &itemHandle, &box);
- GetIText( itemHandle, theText);
-
- SpeakAndShowText(CPDialog, numItems, cdevStorage, theText, animate);
- break;
-
- case kShowIcon:
- (**thePreferences).iconAtStartup = HitCheckBox(CPDialog, item);
- break;
-
- // Speak text synchronously.
- case kNoOverLap:
- (**thePreferences).onlyPlayOne = HitCheckBox(CPDialog, item);
- break;
-
- // Play the "about" sound.
- case kDecorativeIcon:
- SpeakAndShowText(CPDialog, numItems, cdevStorage, aboutText, animate);
- break;
-
- // Speak phrase faster.
- case kFaster:
- (**thePreferences).theRate += (10 << 16);
- GetDItem( CPDialog, numItems + kText, &itemType, &itemHandle, &box);
- GetIText( itemHandle, theText);
-
- SpeakAndShowText(CPDialog, numItems, cdevStorage, theText, animate);
-
- break;
-
- // Speak phrase slower.
- case kSlower:
- (**thePreferences).theRate -= (10 << 16);
- GetDItem( CPDialog, numItems + kText, &itemType, &itemHandle, &box);
- GetIText( itemHandle, theText);
-
- SpeakAndShowText(CPDialog, numItems, cdevStorage, theText, animate);
- break;
-
- // Lower the pitch
- case kLower:
- (**thePreferences).thePitch -= (1 << 16);
- GetDItem( CPDialog, numItems + kText, &itemType, &itemHandle, &box);
- GetIText( itemHandle, theText);
-
- SpeakAndShowText(CPDialog, numItems, cdevStorage, theText, animate);
- break;
-
- // Raise the pitch
- case kHigher:
- (**thePreferences).thePitch += (1 << 16);
- GetDItem( CPDialog, numItems + kText, &itemType, &itemHandle, &box);
- GetIText( itemHandle, theText);
-
- SpeakAndShowText(CPDialog, numItems, cdevStorage, theText, animate);
- break;
-
- case kAnimate:
- (**thePreferences).animate = HitCheckBox(CPDialog, item);
- break;
-
-
- } // switch
-
- } // DoItemHit
-
-
-
- void DoUpdate( DialogPtr CPDialog,
- short numItems,
- long *cdevStorage)
- {
- #pragma unused( CPDialog, numItems, cdevStorage)
- }
-
- pascal void SpeakAndShowText(DialogPtr CPDialog,
- short numItems,
- long *cdevStorage,
- StringPtr theText,
- Boolean animate)
- {
- short itemType;
- Handle itemHandle;
- Rect box;
- SpeechChannel itsChannel;
- OSErr err = -214;
- callbackinfo itsInfo;
- long ourA5;
- PreferencesHdl thePreferences = (PreferencesHdl)*cdevStorage;
- long timeOut = TickCount() + 180;
- Boolean reallyAnimate = animate;
-
- GetDItem( CPDialog, numItems + kDecorativeIcon, &itemType, &itemHandle, &box);
-
- itsInfo.itsDialog = CPDialog;
-
- // Get the icons for the open/closed mouth
- if ( reallyAnimate) {
- itsInfo.open = GetCIcon( 2000);
- itsInfo.close = GetCIcon( 1000);
- }
- else {
- itsInfo.open = nil;
- itsInfo.close = nil;
- }
- itsInfo.itsRect = box;
-
- // Lock 'em.
- if (itsInfo.open != nil)
- HLock( (Handle) itsInfo.open);
- else
- reallyAnimate = false; // In case we didn't get the icons
-
- if (itsInfo.close != nil)
- HLock( (Handle) itsInfo.close);
- else
- reallyAnimate = false;
-
- // Try for no more than 3 seconds to get a new speech channel.
- while (( err != noErr) && (TickCount() < timeOut))
- err = NewSpeechChannel( &(**thePreferences).theVoice, &itsChannel);
-
- if ( err != noErr) {
- ShowStrNum("\p Could not get speech channel!", err);
- goto ErrExit;
- }
-
- (void)SetSpeechPitch( itsChannel, (**thePreferences).thePitch);
- (void)SetSpeechRate( itsChannel, (**thePreferences).theRate);
-
- // Set the reference constant to point to my struct.
- if ( reallyAnimate ) {
- err = SetSpeechInfo( itsChannel, soRefCon, &itsInfo);
-
- if ( err != noErr) {
- ShowStrNum("\p Could not set speech info!", err);
- goto ErrExit;
- }
- }
-
- // Set the A5 to use when calling my callbacks.
- if ( reallyAnimate ) {
- ourA5 = SetCurrentA5();
- err = SetSpeechInfo( itsChannel, soCurrentA5, &ourA5);
-
- if ( err != noErr) {
- (void)DisposeSpeechChannel( itsChannel);
- ShowStrNum("\p Could not set speech info!", err);
- goto ErrExit;
- }
- }
-
- // Set the end of text callback.
- if ( reallyAnimate ) {
- err = SetSpeechInfo( itsChannel, soSpeechDoneCallBack, &EndOfTextCallback);
-
- if ( err != noErr) {
- (void)DisposeSpeechChannel( itsChannel);
- ShowStrNum("\p Could not set speech info!", err);
- goto ErrExit;
- }
- }
-
- // Set the end of word callback.
- if ( reallyAnimate ) {
- err = SetSpeechInfo( itsChannel, soPhonemeCallBack, &PhonemeCallBack);
-
- if ( err != noErr) {
- (void)DisposeSpeechChannel( itsChannel);
- ShowStrNum("\p Could not set speech info!", err);
- goto ErrExit;
- }
- }
-
- // Finally speak the text.
- err = SpeakText( itsChannel, &theText[1], theText[0]);
-
- if ( err != noErr) {
- (void)DisposeSpeechChannel( itsChannel);
- ShowStrNum("\p Could not set speech info!", err);
- goto ErrExit;
- }
-
- while( SpeechBusy() > 0)
- {} // Wait for the speech to complete.
-
- err = DisposeSpeechChannel( itsChannel);
-
- if ( err != noErr) {
- ShowStrNum("\p Could not dispose channel!", err);
- goto ErrExit;
- }
-
- ErrExit:
-
- if (itsInfo.open != nil)
- DisposeCIcon(itsInfo.open);
- if (itsInfo.close != nil)
- DisposeCIcon(itsInfo.close);
-
-
-
- }
-
-
- OSErr DoActivate(DialogPtr CPDialog,
- short numItems,
- long *cdevStorage)
- {
- OSErr err;
- short i, limit;
- VoiceSpec TheVoice;
- VoiceDescription info;
- short itemType;
- Rect dRect;
- Handle dHandle;
- MenuHandle voiceMenu;
- ControlHandle cntrl;
- PreferencesHdl thePreferences = (PreferencesHdl)*cdevStorage;
- short menuItem = 0;
-
-
- // Get the handle of the voice menu
- GetDItem( CPDialog, kVoiceMenu + numItems, &itemType, (Handle *) &cntrl, &dRect);
- BlockMove( &(**((**cntrl).contrlData)), &voiceMenu, sizeof(MenuHandle));
-
- for ( i = CountMItems(voiceMenu); i > 0; i--)
- DelMenuItem(voiceMenu,1);
-
- // Count the number of voices available.
- err = CountVoices(&limit);
- if (err != noErr ) {
- ShowStrNum("\p No voices available!", err);
- *cdevStorage = cdevGenErr;
- return(cdevGenErr);
- }
-
- // Add each voice to the voice menu.
- for(i = 1; i <= limit; i++)
- {
- err = GetIndVoice(i, &TheVoice);
- if (err != noErr ) {
- ShowStrNum("\p Can't GetIndVoice!", err);
- goto ErrExit;
- }
- err = GetVoiceDescription( &TheVoice, &info, sizeof(VoiceDescription));
- if (err != noErr ) {
- ShowStrNum("\p Can't GetVoiceDescription!", err);
- goto ErrExit;
- }
- AppendMenu(voiceMenu, info.name);
-
- // Save the one in use
- if ( TheVoice == (**thePreferences).theVoice)
- menuItem = i;
-
- }
-
- // Set the default voice along with its speech rate and pitch.
- if ( menuItem == 0) {
- SpeechChannel itsChannel;
- long timeOut = TickCount() + 180;
- menuItem = 1;
- err = GetIndVoice(1, &(**thePreferences).theVoice);
-
- if ( err != noErr) {
- err = -1;
- // Try for no more than 3 seconds to get a new speech channel.
- while (( err != noErr) && (TickCount() < timeOut))
- err = NewSpeechChannel( &(**thePreferences).theVoice, &itsChannel);
-
- if ( err == noErr) {
- (void) GetSpeechPitch( itsChannel, &(**thePreferences).thePitch);
- (void) GetSpeechRate( itsChannel, &(**thePreferences).theRate);
- (void) DisposeSpeechChannel( itsChannel);
- }
-
- }
- }
-
- // Set the current voice being used.
- SetCtlMax( cntrl, CountMItems( voiceMenu));
- SetCtlValue( cntrl, menuItem);
-
- // Set the on/off radio cluster
- if ((**thePreferences).on_off)
- HitRadioButton(CPDialog, numItems + kOnButton, numItems + kOnButton, kOnButton, kOffButton);
- else
- HitRadioButton(CPDialog, numItems + kOffButton, numItems + kOffButton, kOnButton, kOffButton);
-
- // Set the show icon button
- GetDItem(CPDialog, numItems + kShowIcon, &itemType, &dHandle, &dRect);
- if ( (**thePreferences).iconAtStartup)
- SetCtlValue( (ControlHandle) dHandle, 1);
- else
- SetCtlValue( (ControlHandle) dHandle, 0);
-
- // Set the "play one at a time" button
- GetDItem(CPDialog, numItems + kNoOverLap, &itemType, &dHandle, &dRect);
- if ( (**thePreferences).onlyPlayOne)
- SetCtlValue( (ControlHandle) dHandle, 1);
- else
- SetCtlValue( (ControlHandle) dHandle, 0);
-
- // Set the "play one at a time" button
- GetDItem(CPDialog, numItems + kAnimate, &itemType, &dHandle, &dRect);
- if ( (**thePreferences).animate)
- SetCtlValue( (ControlHandle) dHandle, 1);
- else
- SetCtlValue( (ControlHandle) dHandle, 0);
-
-
- // Set the text in the dialog.
- GetDItem(CPDialog, numItems + kText, &itemType, &dHandle, &dRect);
- SetIText( dHandle, (**thePreferences).theText);
-
- return( noErr);
-
- ErrExit:
- if ( *cdevStorage != nil)
- DisposeHandle( (Handle) *cdevStorage);
-
- *cdevStorage = cdevGenErr;
- return(cdevGenErr);
-
- }
-
-
- typedef pascal OSErr (*SetPreferencesProcPtr)( void);
-
- OSErr WritePreferences(PreferencesHdl preferences)
- {
- short err;
- SetPreferencesProcPtr addr = 0x01;
-
- // Write the preferences to disk.
- err = WriteToPrefs( preferences, kBeepSayPrefs);
- if (err != noErr)
- return ( err);
-
- // Call the Gestalt function which returns a proc pointer to the routine which
- // will update the extensions global variables from this preferences file.
- err = Gestalt( cKBPSAChangedResource,(long *) &addr);
- if (err != noErr) {
-
- ShowStr("\p Lil´ BeepSay is not Installed!");
- return ( noErr);
- }
-
-
- // Make the extensions update themselves.
- err = (*addr)();
-
- return ( err);
- }
-
-
-